package com.dhcc.core.modules.msgcenter.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.msgcenter.cache.MsgCache;
import com.dhcc.core.modules.msgcenter.entity.MsgUserSetting;
import com.dhcc.core.modules.msgcenter.service.IMsgUserSettingService;


/**
 * 用户提醒设置
 * 
 * @ClassName: MsgUserSettingController
 * @Description: TODO
 * @author cyf
 * @date 2018-11-15 15:02:16
 */
@Controller
@RequestMapping("${sys.backendPath}/msgcenter/usersetting")
public class MsgUserSettingController extends BaseController {

	private String PREFIX = "/backend/msgcenter/usersetting/";

	@Autowired
	private IMsgUserSettingService msgUserSettingService;
	
    /**
     * 跳转到用户提醒设置列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        List<MsgUserSetting> list = msgUserSettingService.loadUserSetting(this.getLoginUserId());
        model.addAttribute("settings", list);
        model.addAttribute("pushTypes", MsgCache.me().getPushTypes());
        return PREFIX + "usersetting.html";
    }
    @RequestMapping(value = "/addType")
    @ResponseBody
    public Object addType(@RequestParam Long settingId,@RequestParam String pushType) {
        MsgUserSetting setting = msgUserSettingService.selectById(settingId);
        setting.setPushTypes(setting.getPushTypes()+","+pushType);
        setting.updateById();
        return R.ok();
    }
    @RequestMapping(value = "/delType")
    @ResponseBody
    public Object delType(@RequestParam Long settingId,@RequestParam String pushType) {
        MsgUserSetting setting = msgUserSettingService.selectById(settingId);
        String[] types = ArrayUtil.stringToArray(setting.getPushTypes(), ArrayUtil.COMMA);
        if(CommonUtil.isNotEmpty(types)){
            String pushTypes = "";
            for (int i = 0; i < types.length; i++) {
                if(CommonUtil.isNotEmpty(types[i])&&CommonUtil.notEquals(types[i], pushType)){
                    pushTypes += (i==0?"":",")+types[i];
                }
            }
            setting.setPushTypes(pushTypes);
            setting.updateById();
        }
        return R.ok();
    }
	
}
